/**
 * @author benny.zheng
 * @data 2016-06-06
 * @description 本文件用于方便复制粘贴UI模块之用，请更新这里的说明
 *              另外，考虑到一般是放在js/src/pages/page-name/main.js，因此使用../../
 *              如果不是这个目录，请更改成正确的相对路径
 */
//----------------require--------------

var viewport = require("mlib/dom/viewport"); // viewport
var base = require("mlib/comp/base"); // 基础对象
var parseModule = require("mlib/dom/parseModule"); // 页面模块自动解析
var wechat = require("../plugin/wx/wx-api");

var round = require("../plugin/util/round");
var date = require("../plugin/util/date");

var ajax = require("mlib/io/ajax");
var virtualLink = require("mlib/util/virtualLink");
var follow = require("../public/js/follow");
var closest = require("mlib/dom/closest");
var lazyload = require("mlib/dom/lazyload");
var Alert = require("../layer/weuialert");
var touch = require("mlib/evt/touch");


var listRender = require("./list.ejs");
var emptyRender = require("./empty.ejs");

module.exports = function(node, opts) {
    //-----------声明模块全局变量-------------
    var nodeList = null; // 存储所有关键节点
    var that = base();
    var data = null;


    var loadobj = null;
    var listelem = null;

	var statusCode = {
		"NORMAL":"use",
		"FREEZE":"invalid",
		"CONSUMED":"used",
		"EXPIRE":"invalid",
		"GIFTING":"donationing",
		"GIFT_SUCC":"donationed",
		"GIFT_TIMEOUT":"invalid",
		"UNAVAILABLE":"invalid"
	};
	var invalidCode = {
		"NORMAL":"未生效",
		"FREEZE":"已冻结",
		"CONSUMED":"已使用",
		"EXPIRE":"已过期",
		"GIFTING":"转赠中",
		"GIFT_SUCC":"已转赠",
		"DELETE":"已删除",
		"GIFT_TIMEOUT":"券超时",
		"UNAVAILABLE":"无效券"
	};
    //-------------事件响应声明---------------
    var evtFuncs = {
	        getVoucher:function(evt){
	            follow();
	        },
		    getdata:function(){
		        var  tagparam = data.tag ? data.tag : 'effective';	
				var  requrl = "/index.php?r=CouponApi/code&tag="+tagparam;
					ajax({
						url:requrl ,
						data: {}, 
						method: "post",
						onSuccess: function(result) {
							that.unLock();
							if(result.status==1){
								nodeList.couponpage.style.visibility = 'visible';
								if(result.data.length==0){
									//nodeList.couponpage.parentElement.removeChild(nodeList.couponpage);
									listelem.parentElement.insertAdjacentElement("afterend",nodeList.footer);
									listelem.insertAdjacentHTML('beforeend','<div class="emptybox"><div class="empty-icon"><i class="icon-nocoupon"></i></div><div class="empty-text">暂无优惠券~</div></div>');								
								}else{
									
									listelem.insertAdjacentHTML('beforeend',listRender({"codelist":result.data,"statusCode":statusCode,"invalidCode":invalidCode,"nowTime":Math.round(new Date().getTime()/1E3)}));
								}
							}							 				
						},
						onError: function(xmlHttp) {
						}
					});
			},
			loadlist:function(){

					evtFuncs.getdata(1);

			},
			followMe:function(){
				follow(null,true);
			}
	}

    //-------------子模块实例化---------------
    var initMod = function() { 
        listelem = nodeList.listelem;
        document.title = data.tag == "invalid" ? "失效优惠券列表" : "优惠券列表";
		virtualLink(function(evt){
			location.href = evt.target.getAttribute("data-href");
		});
		wechat('off').init();	


    }

    //-------------绑定事件------------------
    var bindEvents = function() {
    	touch.on(document.body, 'tap', ".followme", evtFuncs.getVoucher);
		evtFuncs.loadlist(true);
	}

    //-------------自定义函数----------------
    var custFuncs = {

    }

    //-------------一切从这开始--------------
    var init = function(_data) {

        data = _data;

        // 找到所有带有node-name的节点
        nodeList = parseModule(node);
        // 子模块实例化
        initMod();
        // 绑定事件
        bindEvents();


    }

    //---------------暴露API----------------
    that.init = init;

    return that;
};